package com.jyy.prefabricated.service;

import com.jyy.prefabricated.entity.QuestionBankCategory;
import com.jyy.prefabricated.entity.ProjectCategory;
import com.jyy.prefabricated.entity.QuestionBankCategory;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.mapper.QuestionBankCategoryMapper;
import com.jyy.prefabricated.mapper.QuestionBankCategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @version 1.0.0
 * @author: zjj
 */

@Service
public class QuestionBankCategoryService {

    @Autowired
    private QuestionBankCategoryMapper questionBankCategoryMapper;

    /**
     * 新增
     * @param questionBankCategory
     * @return
     */
    public int insert(QuestionBankCategory questionBankCategory){
        questionBankCategoryMapper.insert(questionBankCategory);
        return questionBankCategory.getId();
    }

    /**
     * 更新
     * @param questionBankCategory
     * @return
     */
    public int update(QuestionBankCategory questionBankCategory){
        if(questionBankCategory.getId() == null){
            throw new CustomException("参数id不能为空");
        }
        checkCategoryLevel(questionBankCategory);
        return questionBankCategoryMapper.updateByPrimaryKeySelective(questionBankCategory);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    public int delete(Integer id){
        return questionBankCategoryMapper.deleteByPrimaryKey(id);
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    public QuestionBankCategory selectById(Integer id){
        return questionBankCategoryMapper.selectByPrimaryKey(id);
    }

    /**
     * 查询所有分类列表
     * @return
     */
    public List<QuestionBankCategory> selectAll(){
        return questionBankCategoryMapper.selectAll();
    }

    /**
     * 根据参数查询列表
     * @param category 名称
     * @param pid 父级id
     * @return
     */
    public List<QuestionBankCategory> selectByParams(String category,Integer pid , Integer trainingOrganizationId){
        return questionBankCategoryMapper.selectByParams(category, pid , trainingOrganizationId);
    }

    /**
     * 校验层级
     * @param category
     */
    public void checkCategoryLevel(QuestionBankCategory category){
        List<QuestionBankCategory> categories = selectAll();
        List<QuestionBankCategory> son = findCategoryIdByPid(category.getId(), categories);
        List<Integer> ids = son.stream().map(e->e.getId()).collect(Collectors.toList());
        if(ids.contains(category.getPid()) || category.getId().equals(category.getPid())){
            throw new CustomException("当前分类的父类不能为它的子分类和它本身");
        }
    }

    /**
     * 根据父级id所有子级分类id
     * @param pid
     * @param categories
     * @return
     */
    public List<QuestionBankCategory> findCategoryIdByPid(Integer pid, List<QuestionBankCategory> categories){
        List<QuestionBankCategory> list = new ArrayList<>();
        categories.stream().filter(e->pid.equals(e.getPid())).forEach(item->{
            list.add(item);
            List<QuestionBankCategory> son = findCategoryIdByPid(item.getId(),categories);
            list.addAll(son);
        });
        return list;
    }
}
